package com.hyj.shop.project.article.service.impl;

import com.hyj.shop.project.article.domain.LikeArticle;
import com.hyj.shop.project.article.mapper.ArticleMapper;
import com.hyj.shop.project.article.service.ArticleService;
import com.hyj.shop.project.index.domain.Article;
import com.hyj.shop.project.utils.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public AjaxResult addArticleReadCount(int articleId) {
            int flag = articleMapper.addArticleReadCount(articleId);
            if(flag==1){
              AjaxResult ajaxResult =  AjaxResult.success();
              return  ajaxResult;
            }else {
                AjaxResult ajaxResult =  AjaxResult.error();
                return  ajaxResult;
            }

    }

    @Override
    public AjaxResult addArticleLikeCount(LikeArticle likeArticle) {
        int flag2 =articleMapper.addLikeArticle(likeArticle.getUserId(),likeArticle.getArticleId());
        int flag = articleMapper.addArticleLikeCount(likeArticle);
        System.out.println(flag);
        if(flag==1){
            AjaxResult ajaxResult =  AjaxResult.success();
            return  ajaxResult;
        }else {
            AjaxResult ajaxResult =  AjaxResult.error();
            return  ajaxResult;
        }
    }

    @Override
    public AjaxResult articleIsLike(int userId, int articleId) {
        System.out.println(userId);
        System.out.println(articleId);
        LikeArticle article = articleMapper.articleIsLike(userId,articleId);
        System.out.println("查到的文章");
        System.out.println(article);
       AjaxResult ajaxResult= AjaxResult.success();
        if(article!=null){
            ajaxResult.put("data",true);
        }else {
            ajaxResult.put("data",false);
        }
        return ajaxResult;
    }

    @Override
    public AjaxResult nextArticle(int articleId) { // 下一篇文章
        try{
            Article nextArticl = articleMapper.nextArticle(articleId);
            Article preArticl = articleMapper.preArticle(articleId);
            ArrayList list =new ArrayList<Article>();
            list.add(preArticl);
            list.add(nextArticl);
            AjaxResult ajaxResult=  AjaxResult.success();
            ajaxResult.put("data",list);
            return ajaxResult;
        }catch(Exception e){
            AjaxResult ajaxResult =AjaxResult.error(e.toString());
            return ajaxResult;
        }
    }
}
